# -*- coding:UTF-8 -*-
import requests
import json
import traceback
from infrastructure.util.logger import get_logger_multi_file
from config.config import config, ValueTypeEnum


logger = get_logger_multi_file()
timeout_default = config.get("request", "timeout", value_type=ValueTypeEnum.INT)
timeout_default =timeout_default if timeout_default else 3

headers = {
    "Content-Type": "application/xbrl_sse;charset=UTF-8",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.50"
    }


def get(url:str, data:dict, timeout=3) -> dict:
    try:
        logger.debug("访问连接:<{}>, 参数：{}， 请求头：{}".format(url, str(data), str(headers)))
        timeout = timeout if timeout else timeout_default
        timeout = timeout if timeout else 3
        response = requests.get(url=url, params=data, headers=headers, timeout=timeout, verify=False)
        logger.debug("访问连接:<{}>, 返回数据：{}".format(url, str(response)))
        return json.loads(response.text)
    except Exception:
        logger.error("访问连接:<{}>发生错误：{}".format(url, traceback.format_exc()))
    return None


def post(url:str, data:dict, timeout=3) -> dict:
    try:
        logger.debug("访问连接:<{}>, 参数：{}， 请求头：{}".format(url, str(data), str(headers)))
        timeout = timeout if timeout else timeout_default
        timeout = timeout if timeout else 3
        data = json.dumps(data)
        response = requests.post(url=url, data=data, headers=headers, timeout=timeout, verify=False)
        logger.debug("访问连接:<{}>, 返回数据：{}".format(url, str(response)))
        return json.loads(response.text)
    except Exception:
        logger.error("访问连接:<{}>发生错误：{}".format(url, traceback.format_exc()))
    return None


def put(url:str, data:dict, timeout=3) -> dict:
    try:
        logger.debug("访问连接:<{}>, 参数：{}， 请求头：{}".format(url, str(data), str(headers)))
        timeout = timeout if timeout else timeout_default
        timeout = timeout if timeout else 3
        data = json.dumps(data)
        response = requests.put(url, data=data, headers=headers, timeout=timeout, verify=False)
        logger.debug("访问连接:<{}>, 返回数据：{}".format(url, str(response)))
        results = json.loads(response.text)
        return results
    except Exception:
        logger.error("访问连接:<{}>发生错误：{}".format(url, traceback.format_exc()))
    return None


def delete(url:str, data:dict, timeout=3) -> dict:
    try:
        logger.debug("访问连接:<{}>, 参数：{}， 请求头：{}".format(url, str(data), str(headers)))
        timeout = timeout if timeout else timeout_default
        timeout = timeout if timeout else 3
        data = json.dumps(data)
        response = requests.delete(url, data=data, headers=headers, timeout=timeout, verify=False)
        logger.debug("访问连接:<{}>, 返回数据：{}".format(url, str(response)))
        results = json.loads(response.text)
        return results
    except Exception:
        logger.error("访问连接:<{}>发生错误：{}".format(url, traceback.format_exc()))
    return None